<template>
  <div class="add-point-dialog">
    <el-dialog
      v-model="dialogVisible"
      :title="'添加用户'"
      width="50%"
      @open="open"
      @closed="closed"
    >
      <div class="dialog-content">
        <el-form
          ref="ruleFormRef"
          :model="form"
          :rules="rules"
          label-width="80px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户昵称" prop="nickName">
                <el-input
                  v-model="form.nickName"
                  placeholder="请输入用户昵称"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机号码" prop="phonenumber">
                <el-input
                  v-model="form.phonenumber"
                  placeholder="请输入手机号码"
                  maxlength="11"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input
                  v-model="form.email"
                  placeholder="请输入邮箱"
                  maxlength="50"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户名称" prop="userName">
                <el-input
                  v-model="form.userName"
                  placeholder="请输入用户名称"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户密码" prop="password">
                <el-input
                  v-model="form.password"
                  placeholder="请输入用户密码"
                  type="password"
                  maxlength="20"
                  show-password
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户性别">
                <el-select v-model="form.sex" placeholder="请选择性别">
                  <el-option
                    v-for="dict in sexList"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="角色">
                <el-select
                  v-model="form.roleIds"
                  multiple
                  placeholder="请选择角色"
                >
                  <el-option
                    v-for="item in roleOptions"
                    :key="item.roleId"
                    :label="item.roleName"
                    :value="item.roleId"
                    :disabled="item.status == 1"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input
                  v-model="form.remark"
                  type="textarea"
                  placeholder="请输入内容"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit" :loading="isLoading">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { AddUser } from "@/api/system/system";
import { addUser, getAllRole } from "@/api/system";

defineOptions({
  name: "addUser"
});
interface Props {
  modelValue: boolean;
}
const props = defineProps<Props>();
const emits = defineEmits(["update:modelValue", "getList"]);
const dialogVisible = computed({
  get() {
    return props.modelValue;
  },
  set(newValue) {
    emits("update:modelValue", newValue);
  }
});
const sexList = [
  {
    label: "男",
    value: "0"
  },
  {
    label: "女",
    value: "1"
  }
];
// 获取角色列表
const roleOptions = ref([]);
const getRoleList = async () => {
  const res = await getAllRole();
  if (res.code === 200) {
    roleOptions.value = res.rows.map(el => ({
      roleName: el.roleName,
      roleId: el.roleId
    }));
  }
};
const open = async () => {
  getRoleList();
};
// 表单
const ruleFormRef = ref<FormInstance>();
const form = ref<AddUser>({
  userName: "",
  nickName: "",
  password: "",
  phonenumber: "",
  email: "",
  sex: "0",
  status: "0",
  remark: "",
  postIds: [],
  roleIds: [1]
});
const rules = reactive<FormRules<typeof form>>({
  userName: [
    { required: true, message: "用户名称不能为空", trigger: "blur" },
    {
      min: 2,
      max: 20,
      message: "用户名称长度必须介于 2 和 20 之间",
      trigger: "blur"
    }
  ],
  nickName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
  password: [
    { required: true, message: "用户密码不能为空", trigger: "blur" },
    {
      min: 5,
      max: 20,
      message: "用户密码长度必须介于 5 和 20 之间",
      trigger: "blur"
    }
  ],
  email: [
    {
      type: "email",
      message: "请输入正确的邮箱地址",
      trigger: ["blur", "change"]
    }
  ],
  phonenumber: [{ required: true, message: "手机号不能为空", trigger: "blur" }]
});
const closed = () => {
  form.value = {
    userName: "",
    nickName: "",
    password: "",
    phonenumber: "",
    email: "",
    sex: "0",
    status: "0",
    remark: "",
    postIds: [],
    roleIds: [1]
  };
  ruleFormRef.value?.clearValidate();
};
// 添加
const add = async () => {
  const res = await addUser(form.value);
  return res;
};
// 提交
const isLoading = ref(false);
const submit = () => {
  ruleFormRef.value?.validate(async valida => {
    if (valida) {
      isLoading.value = true;
      const res = await add();
      isLoading.value = false;
      if (res.code === 200) {
        dialogVisible.value = false;
        ElMessage.success(` 添加成功`);
        emits("getList");
      }
    }
  });
};
</script>
<style lang="scss" scoped>
:deep(.el-form-item__label) {
  font-weight: 700;
}
</style>
